package com.ay.wiki_plt.controller;

import com.ay.wiki_plt.common.PageBean;
import com.ay.wiki_plt.common.Result;
import com.ay.wiki_plt.domain.Doc;
import com.ay.wiki_plt.domain.dto.DocSaveReq;
import com.ay.wiki_plt.domain.dto.PageRequestDTO;
import com.ay.wiki_plt.service.DocSerivce;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/doc")
@Slf4j
public class DocController {

    @Autowired
    private DocSerivce docSerivce;


    /*查询全部文档*/
    @GetMapping("/list")
    public Result list() {
        log.info("查询全部文档");
        //调用service
        List<Doc> docSerivceList = docSerivce.getDocList();
        return Result.success(docSerivceList);
    }


    /*查询全部文档(未分页)*/
    @GetMapping("/list/{ebookId}")
    public Result list(@PathVariable Long ebookId) {
        log.info("查询全部文档");
        //调用service
        List<Doc> docSerivceList = docSerivce.list(ebookId);
        return Result.success(docSerivceList);
    }

    /*查询全部文档 分页*/
    @GetMapping("/page")
    public Result list(@Valid PageRequestDTO pageRequestDTO,
                       @RequestParam(defaultValue = "") String name) {
        log.info("分页参数:{},{},{}", pageRequestDTO.getPage(), pageRequestDTO.getPageSize(), name);
        PageBean pageBean = docSerivce.list(pageRequestDTO.getPage(),pageRequestDTO.getPageSize(),name);
        return Result.success(pageBean);

    }


    /*保存和修改文档*/
    @PostMapping
    public Result save(@RequestBody DocSaveReq doc){
        log.info("保存和修改文档参数:{}",doc);
        //调用service
        docSerivce.save(doc);
        return Result.success();
    }


    /*
    删除接口 含批量删除 前端传来1,2,3
    */

    @DeleteMapping("/{ids}")
    public Result deleteBatch(@PathVariable List<Long> ids){
        log.info("删除的文档id:{}",ids);
        //调用service
        docSerivce.deleteBatch(ids);
        return Result.success();
    }


    /*文档内容查询*/
    @GetMapping("/find-content/{id}")
    public Result findContext(@PathVariable Long id){
        log.info("查询文档内容参数:{}",id);
        String content =docSerivce.findContent(id);
        return Result.success(content);
    }


    /*点赞按钮功能*/
    @GetMapping("/vote/{id}")
    public Result vote(@PathVariable Long id){
        docSerivce.vote(id);
        return Result.success();
    }

}
